﻿#pragma once
#include "HashTable.h"

namespace bzh
{
	template <class k, class v, class Hash = HashFunc<k>>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const k& operator()(const pair<const k, v>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename buckethash::HashTable<k, pair<const k, v>,
			Hash, MapKeyOfT>::iterator iterator;
		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end();
		}
		iterator find(const k& key)
		{
			return _ht.Find(key);
		}
		bool erase(const k& key)
		{
			return _ht.Erase(key);
		}
		pair<iterator, bool> insert(const pair<k, v>& data)
		{
			return _ht.Insert(data);
		}
		v& operator[](const k& key)////插入/修改/查找
		{
			pair<iterator, bool> ret = _ht.Insert(make_pair(key, v()));
			return ret.first->second;
		}
	private:
		buckethash::HashTable<k, pair<const k,v>, Hash, MapKeyOfT> _ht;
	};
	void test_unordered_map()
	{
		string arr[] = { "苹果", "西瓜", "香蕉", "草莓", "苹果", "西瓜",
					"苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };

		unordered_map<string, int> countMap;
		for (auto& e : arr)
		{
			countMap[e]++;
		}

		for (const auto& kv : countMap)
		{
			cout << kv.first << ":" << kv.second << endl;
		}
	}
}
